local skel = fk.CreateSkill {
  name = "rmt__yuxing",
  tags = {Skill.Lord},
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Start and
    Fk.all_card_types["await_exhausted"] ~= nil and
      player:canUse(Fk:cloneCard("await_exhausted"))
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#rmt__yuxing-invoke"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard("await_exhausted")
    card.skillName = skel.name
    local use = {
      from = player,
      card = card,
    }
    room:useCard(use)
    if player.dead then return end
    local cards = (use.extra_data or {}).rmt__yuxing_discards or {}
    cards = table.filter(cards, function (id)
      return table.contains(room.discard_pile, id)
    end)
    if #cards < 2 then return end
    local put = room:askToGuanxing(player, {
      cards = cards,
      top_limit = {2, 2},
      skill_name = skel.name,
      skip = true,
      area_names = {"Top", "pile_discard"},
      prompt = "#rmt__yuxing-put",
    }).top
    if #put == 2 then
      room:moveCards({
        ids = table.reverse(put),
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = skel.name,
        proposer = player,
      })
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player.seat == 1 then
      local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if use_event then
        local use = use_event.data
        if use.card.trueName == "await_exhausted" and table.contains(use.card.skillNames, skel.name) then
          local cards = {}
          for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard
            -- 别乱写move.skillName求你们了
             and type(move.skillName) == "string" and string.find(move.skillName, "await_exhausted") then
              for _, info in ipairs(move.moveInfo) do
                if table.contains(player.room.discard_pile, info.cardId) then
                  table.insertIfNeed(cards, info.cardId)
                end
              end
            end
          end
          if #cards > 0 then
            cards = player.room.logic:moveCardsHoldingAreaCheck(cards)
            if #cards > 0 then
              event:setCostData(self, {cards = cards})
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local cards = event:getCostData(self).cards
    local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if use_event then
      local use = use_event.data
      use.extra_data = use.extra_data or {}
      use.extra_data.rmt__yuxing_discards = use.extra_data.rmt__yuxing_discards or {}
      table.insertTableIfNeed(use.extra_data.rmt__yuxing_discards, cards)
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__yuxing"] = "聿兴",
  [":rmt__yuxing"] = "主公技，准备阶段，你可以视为使用【以逸待劳】，结算后将因此弃置的两张牌置于牌堆顶。",
  ["#rmt__yuxing-invoke"] = "聿兴：是否视为使用【以逸待劳】？",
  ["#rmt__yuxing-put"] = "请将因【以逸待劳】弃置的两张牌置于牌堆顶",

  ["$rmt__yuxing1"] = "群贤皆蜂至，良策自涌来！",
  ["$rmt__yuxing2"] = "闻良臣之言，如闻丝竹之音，足可慰也！",
}

return skel
